﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xajhzwb.Components.Helper;
using System.IO;

namespace Xajhzwb.Components.Util
{
    public class Config
    {
        private static string configFile = "Xajhzwb.config";
        private static string configPath = Utils.StartupPath + "\\" + configFile;

        private XmlHelper xmlConfig = null;

        #region System

        public string UserName
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/System/UserName");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/System/UserName", value);
                xmlConfig.Save();
            }
        }

        public string PassWord
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/System/PassWord");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/System/PassWord", value);
                xmlConfig.Save();
            }
        }

        public string Language
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/System/Language");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/System/Language", value);
                xmlConfig.Save();
            }
        }

        public string Skin
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/System/Skin");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/System/Skin", value);
                xmlConfig.Save();
            }
        }

        public int DataType
        {
            get
            {
                return int.Parse(xmlConfig.GetNodeValue("Config/System/DataType"));
            }

            set
            {
                xmlConfig.EditNodeValue("Config/System/DataType", value.ToString());
                xmlConfig.Save();
            }
        }

        public string DataConnction
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/System/DataConnction");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/System/DataConnction", value);
                xmlConfig.Save();
            }
        }

        public bool IsAuto
        {
            get
            {
                return bool.Parse(xmlConfig.GetNodeValue("Config/System/IsAuto"));
            }

            set
            {
                xmlConfig.EditNodeValue("Config/System/IsAuto", value.ToString());
                xmlConfig.Save();
            }
        }

        public bool IsMini
        {
            get
            {
                return bool.Parse(xmlConfig.GetNodeValue("Config/System/IsMini"));
            }

            set
            {
                xmlConfig.EditNodeValue("Config/System/IsMini", value.ToString());
                xmlConfig.Save();
            }
        }

        public bool IsAutoUpdate
        {
            get
            {
                return bool.Parse(xmlConfig.GetNodeValue("Config/System/IsAutoUpdate"));
            }

            set
            {
                xmlConfig.EditNodeValue("Config/System/IsAutoUpdate", value.ToString());
                xmlConfig.Save();
            }
        }

        public bool IsLoginRun
        {
            get
            {
                return bool.Parse(xmlConfig.GetNodeValue("Config/System/IsLoginRun"));
            }

            set
            {
                xmlConfig.EditNodeValue("Config/System/IsLoginRun", value.ToString());
                xmlConfig.Save();
            }
        }
        #endregion

        #region NetWork

        public int DelayTime
        {
            get
            {
                return int.Parse(xmlConfig.GetNodeValue("Config/NetWork/DelayTime"));
            }

            set
            {
                xmlConfig.EditNodeValue("Config/NetWork/DelayTime", value.ToString());
                xmlConfig.Save();
            }
        }

        public int ReadTimeout
        {
            get
            {
                return int.Parse(xmlConfig.GetNodeValue("Config/NetWork/ReadTimeout"));
            }

            set
            {
                xmlConfig.EditNodeValue("Config/NetWork/ReadTimeout", value.ToString());
                xmlConfig.Save();
            }
        }

        public string UserAgent
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/NetWork/UserAgent");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/NetWork/UserAgent", value.ToString());
                xmlConfig.Save();
            }
        }

        public bool IsProxy
        {
            get
            {
                return bool.Parse(xmlConfig.GetNodeValue("Config/NetWork/IsProxy"));
            }

            set
            {
                xmlConfig.EditNodeValue("Config/NetWork/IsProxy", value.ToString());
                xmlConfig.Save();
            }
        }

        public string ProxyServer
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/NetWork/ProxyServer");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/NetWork/ProxyServer", value.ToString());
                xmlConfig.Save();
            }
        }

        public string ProxyPort
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/NetWork/ProxyPort");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/NetWork/ProxyPort", value.ToString());
                xmlConfig.Save();
            }
        }

        public string ProxyUser
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/NetWork/ProxyUser");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/NetWork/ProxyUser", value.ToString());
                xmlConfig.Save();
            }
        }

        public string ProxyPass
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/NetWork/ProxyPass");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/NetWork/ProxyPass", value.ToString());
                xmlConfig.Save();
            }
        }

        #endregion

        #region Others

        public string RecGet
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/Others/RecGet");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/Others/RecGet", value.ToString());
                xmlConfig.Save();
            }
        }

        public string RecGlobal
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/Others/RecGlobal");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/Others/RecGlobal", value.ToString());
                xmlConfig.Save();
            }
        }

        public string RecWebPost
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/Others/RecWebPost");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/Others/RecWebPost", value.ToString());
                xmlConfig.Save();
            }
        }

        public string RecPlan
        {
            get
            {
                return xmlConfig.GetNodeValue("Config/Others/RecPlan");
            }

            set
            {
                xmlConfig.EditNodeValue("Config/Others/RecPlan", value.ToString());
                xmlConfig.Save();
            }
        }

        //public string RecPlanSel
        //{
        //    get
        //    {
        //        return xmlConfig.GetNodeValue("Config/Others/RecPlanSel");
        //    }

        //    set
        //    {
        //        xmlConfig.EditNodeValue("Config/Others/RecPlanSel", value.ToString());
        //        xmlConfig.Save();
        //    }
        //}

        #endregion

        //public int RecMg
        //{
        //    get
        //    {
        //        return xmlConfig.GetNodeValue("Xajhzwb/Config/RecMg");
        //    }
        //}

        public Config()
        {
            if(!File.Exists(configPath))
            {
                this.NewConfigFile();
            }

            xmlConfig = new XmlHelper(configFile);
        }

        public void NewConfigFile()
        {
            string strXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
                       "<Config>" +
                       "  <System>" +
                       "  </System>" +
                       "  <NetWork>" +
                       "  </NetWork>" +
                       "  <Others>" +
                       "  </Others>" +
                       "</Config>";

            xmlConfig = new XmlHelper();
            xmlConfig.NewXmlFile(configPath, strXml);

            this.NewConfig();
        }

        public void NewConfig()
        {
            // System
            xmlConfig.InsertElement("Config/System", "UserName", "");
            xmlConfig.InsertElement("Config/System", "PassWord", "");
            xmlConfig.InsertElement("Config/System", "Language", "Chinese");
            xmlConfig.InsertElement("Config/System", "Skin", "");
            xmlConfig.InsertElement("Config/System", "DataType", "1");
            xmlConfig.InsertElement("Config/System", "DataConnction",
                                    string.Format("provider=microsoft.jet.oledb.4.0;data source={0}\\ET.mdb;", Utils.StartupPath));
            xmlConfig.InsertElement("Config/System", "IsMini", "False");
            xmlConfig.InsertElement("Config/System", "IsAuto", "True");
            xmlConfig.InsertElement("Config/System", "IsAutoUpdate", "True");
            xmlConfig.InsertElement("Config/System", "IsLoginRun", "False");

            // NetWork
            xmlConfig.InsertElement("Config/NetWork", "DelayTime", "30");
            xmlConfig.InsertElement("Config/NetWork", "ReadTimeout", "120");
            xmlConfig.InsertElement("Config/NetWork", "UserAgent", "");
            xmlConfig.InsertElement("Config/NetWork", "IsProxy", "False");
            xmlConfig.InsertElement("Config/NetWork", "ProxyServer", "");
            xmlConfig.InsertElement("Config/NetWork", "ProxyPort", "8080");
            xmlConfig.InsertElement("Config/NetWork", "ProxyUser", "");
            xmlConfig.InsertElement("Config/NetWork", "ProxyPass", "");

            // Others
            xmlConfig.InsertElement("Config/Others", "RecGet", "");
            xmlConfig.InsertElement("Config/Others", "RecGlobal", "");
            xmlConfig.InsertElement("Config/Others", "RecWebPost", "");
            xmlConfig.InsertElement("Config/Others", "RecPlan", "");

            xmlConfig.Save();
        }



    }
}
